<?php
class ProductqrAction extends CommonAction {
	public function _initialize(){
	    parent::_initialize();
		
		
		
		
		$this->dao = M('Product');
		$this->suppierids = M('Supplier_user') -> Field('supplier_id') -> where(array('user_id' => session(C('USER_AUTH_KEY') ))) -> buildSql();
	    $this->is_suppierids = M('Supplier_user') -> Field('supplier_id') -> where(array('user_id' => session(C('USER_AUTH_KEY') ))) -> count();
		
	}
	  
	function index(){ 

		if(C('URL_MODEL') == 2){
			 $jumpUrl_ = str_replace('index.php/','',__SELF__);
		}else{
			 $jumpUrl_ = __SELF__;
		}
		session('jumpUrl_', $jumpUrl_);
		
		$this->listmap ="1 and n.status = -1 and n.site_id=".$this->siteid;
		//$this->listorder ="sort,id desc";
		$category = D('Product_category');
        if(isset($_GET['group_type'])&&$_GET['group_type']!='all'){
            $s_limit=' and type = '.$_GET['group_type'];
			$this->group_type=$_GET['group_type'];
        }else{
            $s_limit='';
        }
		if(!session(C('ADMIN_AUTH_KEY') )){
			$this->listmap .= ' and supplier_id in('.$this->suppierids.')';
			$this->suppier = M('Supplier') -> where('site_id='.$this->siteid.' and id in('.$this->suppierids.')'.$s_limit) -> select();
		   //if($this->suppier){
			   foreach($this->suppier as $k => $v){
				   D('Supplier') -> createqr($v['id']);
			   }
		  // }
		}else{
			$this->suppier = M('Supplier') -> where('site_id='.$this->siteid.$s_limit) -> select();
		}
		//供货商列表
		//dump(M('Supplier') -> getLastSql());
		if(isset($_GET['category_id'])&&intval($_GET['category_id'])>0){
			$csql = M('Category_goods') -> where(array('category_id' => intval($_GET['category_id']))) -> Field('product_id as id') -> buildSql();
			$cateids = $category -> getAllIDBypid(intval($_GET['category_id']), $this->siteid);
			$this->listmap.=" and (n.category_id in(".implode(',',$cateids).") or n.category_id in(".$csql."))";
			$this->category_id=intval($_GET['category_id']);
		}
		if(isset($_GET['status'])&&$_GET['status']!='all'){
			$this->listmap.=" and n.status = ".intval($_GET['status']);
			$this->status = intval($_GET['status']);
		}
		
		if(isset($_GET['supplier_id'])&&intval($_GET['supplier_id'])>0){
			$this->listmap.=" and n.supplier_id=".intval($_GET['supplier_id']);
			$this->assign('supplier_id', intval($_GET['supplier_id']));
		}
		 
		
		//搜索标题
		if(isset($_GET['keywords'])&& $_GET['keywords']!=''){
			$keywords=$_GET['keywords'];
			$_GET['keywords']=urlencode($_GET['keywords']);
			$this->listmap.=" and n.title like '%{$keywords}%'";
			$this->keywords=$keywords;
			
			$suppliersql = M('Supplier') -> where(array('susername' => array('like', '%'.$keywords.'%'))) -> Field('id') -> buildSql();
			//dump(M('Supplier') -> where(array('susername' => array('like', '%'.$keywords.'%'))) -> Field('id') ->select());
			
			//dump(M('Supplier') -> getLastSql());
			
			$this->listmap.=" or (supplier_id in(".$suppliersql."))";
			//dump($this->listmap);
		}
		
		$this->categoryList=M('Product_category')->where(array('site_id'=>$this->siteid))->select();
        $this->_list();
		
		$pid_list=$category->where(array('pid'=>0,'site_id'=>$this->siteid))->select();
		
		foreach($pid_list as $k=>$v){
			$pid_list[$k]['sub']=$category->getListToArray1($v['id'],$this->siteid);
		}
		$this->assign('pid_list',$pid_list);
        $myid=D('User')->where(' id = '.session(C('USER_AUTH_KEY')))->getField('member_id');
        $this->levelnum=D('Member')->where('id = '.$myid)->getField('level');
		if(isMobile()){
			$this->display('indexMobile');
		}else{
			$this->display();
		}
	}
	
	function junior_page(){
	    
	
	
		if(C('URL_MODEL') == 2){
			 $jumpUrl_ = str_replace('index.php/','',__SELF__);
		}else{
			 $jumpUrl_ = __SELF__;
		}
		session('jumpUrl_', $jumpUrl_);
		
		
		$this->listmap ="1 and n.site_id=".$this->siteid;
		//$this->listorder ="sort,id desc";
		$category = D('Product_category');
		if(!session(C('ADMIN_AUTH_KEY') ) && $this->is_suppierids > 0){
			$this->listmap .= ' and supplier_id in('.$this->suppierids.')';
			$this->suppier = M('Supplier') -> where('site_id='.$this->siteid.' and id in('.$this->suppierids.')') -> select();
			
		}else{
			$this->suppier = M('Supplier') -> where('site_id='.$this->siteid) -> select();
		}
		
		
		
		
		
		
		//供货商列表 
		
		//dump(M('Supplier') -> getLastSql());
		
		//$this->suppier = M('Supplier') -> where('id in('.$this->suppierids.')') -> select();
		//$this->listorder="sort,id desc";
		if(isset($_GET['category_id'])&&intval($_GET['category_id'])>0){
			$csql = M('Category_goods') -> where(array('category_id' => intval($_GET['category_id']))) -> Field('product_id as id') -> buildSql();
			$cateids = $category -> getAllIDBypid(intval($_GET['category_id']), $this->siteid);
			$this->listmap.=" and (n.category_id in(".implode(',',$cateids).") or n.category_id in(".$csql."))";
			//dump($cateids);
			$this->category_id=intval($_GET['category_id']);
			
			
			//$this->listmap.=" and n.category_id=".intval($_GET['category_id']);
			//$this->category_id=intval($_GET['category_id']);
		}
		
		
		if(isset($_GET['status'])){
			$this->listmap.=" and n.status = ".intval($_GET['status']);
			$this->status = intval($_GET['status']);
		}
		
		
		
		
		if(isset($_GET['supplier_id'])&&intval($_GET['supplier_id'])>0){
			$this->listmap.=" and n.supplier_id=".intval($_GET['supplier_id']);
			$this->assign('supplier_id', intval($_GET['supplier_id']));
		}
		 
		
		//搜索标题
		if(isset($_GET['keywords'])&& $_GET['keywords']!=''){
			$keywords=$_GET['keywords'];
			$_GET['keywords']=urlencode($_GET['keywords']);
			$this->listmap.=" and n.title like '%{$keywords}%'";
			$this->keywords=$keywords;
		}
		
		$this->categoryList=M('Product_category')->where(array('site_id'=>$this->siteid))->select();
        $this->_list();
		
		$pid_list=$category->where(array('pid'=>0,'site_id'=>$this->siteid))->select();
		
		foreach($pid_list as $k=>$v){
			$pid_list[$k]['sub']=$category->getListToArray1($v['id'],$this->siteid);
		}
		$this->assign('pid_list',$pid_list);

		if(isMobile()){
			$this->display('indexMobile');
		}else{
			$this->display('index');
		}
	}
	
	
	
	
	function selectProduct(){
		$this->listmap="1 and n.site_id=".$this->siteid;
		$this->listorder="sort,id";
		if(isset($_GET['category_id'])&&intval($_GET['category_id'])>0){
			$this->listmap.=" and n.category_id=".intval($_GET['category_id']);
			$this->category_id=intval($_GET['category_id']);
		}
		
		//搜索标题
		if(isset($_GET['keywords'])&& $_GET['keywords']!=''){
			$keywords=$_GET['keywords'];
			$_GET['keywords']=urlencode($_GET['keywords']);
			$this->listmap.=" and n.title like '%{$keywords}%'";
			$this->keywords=$keywords;
		}
		$relateids = $this->dao -> where(array('id' => $this->_get('product_id'), 'site_id' => $this->siteid)) -> getField('relateids');
		$this->selectedid = explode(',', $relateids);
		//dump($selectedid);
		$this->categoryList=M('Product_category')->where(array('site_id'=>$this->siteid))->select();
        $this->_list();
		$this->display();
	}
	
	
	
	
	//删除商品
	public function delete(){
		if($this->_get('id')){
		    $id=$this->_get('id');
			$stockModel=M("Product_stock");
			$attrModel=M("Product_attr");
			//删除旧记录
			$stockModel->where(array('product_id'=>$id))->delete();
			$attrModel->where(array('product_id'=>$id))->delete();
			
			parent::delete();
		}
	}
	
	public function _list(){
		//计算总页数
		$count=$this->dao->alias('n')->join('left join __PRODUCT_CATEGORY__ c ON n.category_id= c.id')->Field('n.*,c.title as category_name')->where($this->listmap)->count();
		import ( 'ORG.Util.Page' );
		//实例化分页类
		$p=new Page($count,$this->pagesize);
		$list = $this->dao->alias('n')->join('left join __PRODUCT_CATEGORY__ c ON n.category_id= c.id')->Field('n.*,c.title as category_name, c.name as cate_name')->where($this->listmap)->order($this->listorder)->limit($p->firstRow.',
'.$p->listRows)->select();
        $categorydb = M('Product_category');
	  foreach($list as $k => $v){
		  $csql = M('Category_goods') -> where(array('product_id' => intval($v['id']))) -> Field('category_id as id') -> buildSql();
		  $list[$k]['extendcategory'] = $categorydb -> where("id in(".$csql.")") -> select();
	  }
        $this->list = $list;
        $this->pages=$p->show();
	}
	
	public function add(){
		if(!session(C('ADMIN_AUTH_KEY') ) && $this->is_suppierids > 0){
			$this->listmap .= ' and supplier_id in('.$this->suppierids.')';
			$this->SupplierList = M('Supplier') -> where('site_id='.$this->siteid.' and id in('.$this->suppierids.')') -> select();
			
		}else{
			$this->SupplierList = M('Supplier') -> where(array('site_id' => $this->siteid)) -> select();
		}
		//供货商
		
		
		//购买后送优惠券
		$this->couponList = M('Coupon') ->where(array('type' => 1, 'site_id' => $this->siteid)) -> select();
		if(session(C('USER_AUTH_KEY') ) == 1){
				$this->role_list = M('Role')-> where(array('user_id' => session(C('USER_AUTH_KEY')))) -> select();
			}
		//购买后送服务卡
		$this->serviceccardList = M('servicecard') ->where(array('status' => 1, 'site_id' => $this->siteid)) -> select();
		
		$this->categorylist = D('Product_category') -> getListToArray1(0,$this->siteid);//dump($this->categorylist);
		$this->cate_list=D('Product_category')->getListToArray1(0,$this->siteid);
		$this->brandList = M('Brand') -> where(array('site_id' => $this->siteid)) -> select();
		//固定栏目及字段扩展
		$SiteModel=M('Site');
		$curTemplate=$SiteModel->where('id='.$this->siteid)->Field("productExtendsField as productExtendsField")->find();
		$this->extendsField=json_decode($curTemplate['productExtendsField'],true);
		parent::add();
	}
	
	public function insert(){
		if(isset($_POST)){
			
				$_POST['status'] = -1;
				

			
			
			if($this->_post('mimg_url')){
				foreach($_POST['mimg_url'] as $k => $v){
					if($v == ''){
						unset($_POST['mimg_url'][$k]);
					}
				}
				$_POST['mimg_url']=implode(',',$_POST['mimg_url']);
				$_POST['site_id']=$this->siteid;
				
			}else{
				$_POST['site_id']=$this->siteid;
				//$_POST['extendsField']=JSON($_POST['extendsField']);
			}
			
			if($this->_post('starttime') != ''){
				$_POST['starttime'] = strtotime($this->_post('starttime'));
			}
			if($this->_post('timeout') != ''){
				$_POST['timeout'] = strtotime($this->_post('timeout'));
			}
			if($_POST['supplier_id'] > 0){
				//查询这个供货商，把它的坐标写到商品表中
				$supplier = M('Supplier') -> where(array('id' => $_POST['supplier_id'])) -> find();
				$_POST['baidumapX'] = $supplier['baidumapX'];
				$_POST['baidumapY'] = $supplier['baidumapY'];
				$_POST['geohash'] = $supplier['geohash'];
			}
			$this->dao->create(); 
			$result=$this->dao->add();
			
			if(strtolower(MODULE_NAME) != 'site' && $result > 0){
			  $this->insert_data = $this->dao->where(array('id' => $result))->find();
			  $this->insert_relate();
			}
			
			
			$cgdb = M('Category_goods');
				
				//保存扩展分类
				foreach($_POST['category_goods'] as $k){
					$cgdb -> data(array('category_id' => $k, 'product_id' => $result)) -> add();
				}
			//添加完返回ID号,如果有扩展数据,继续添加
			if(isset($_POST['extendsField'])&&$result>0){
				
				
				
				
				$flag=true;
				$newsexdb=M('Product_extends');
				foreach($_POST['extendsField'] as $k=>$v){
					$d['product_id']=$result;
					$d['name']=$k;
					$d['val']=$v;
					if(!$newsexdb->data($d)->add()){
						$flag=false;
					}
				}
				
				//如果添加扩展成功提示信息
				if($flag){
					$this->success("成功添加",U('index'));
				}else{
					$this->error("错误了,可能是数据库服务繁忙,请稍候重试");
				}
			}elseif($result>0){
				$this->success("成功添加",U('index'));
			}else{
				$this->error("错误了,可能是数据库服务繁忙,请稍候重试");
			}
		}
	}
	
	public function edit(){
		if($this->_get('id')){
			//供货商
			$this->categorylist = D('Product_category') -> getListToArray1(0,$this->siteid);
			//扩展分类
			$this->scategorylist = M('category_goods') -> where(array('product_id' => $this->_get('id'))) -> select();
			
			if(session(C('USER_AUTH_KEY')) == 1){
				$this->role_list = M('Role')-> where(array('user_id' => session(C('USER_AUTH_KEY')))) -> select();
			}

			$categorylistids = array();
			foreach($this->scategorylist as $k => $v){
				$categorylistids[] = $v['category_id'];
			}
			$this->brandList = M('Brand') -> where(array('site_id' => $this->siteid)) -> select();

			//购买后送优惠券
		    $this->couponList = M('Coupon') ->where(array('type' => 1, 'site_id' => $this->siteid)) -> select();
			//购买后送服务卡
		    $this->serviceccardList = M('servicecard') ->where(array('status' => 1, 'site_id' => $this->siteid)) -> select();
			
			$this->getLevelNameList = D('Member') -> getLevelNameList();
			
			$this->assign('categorylistids', $categorylistids);
			
			if(!session(C('ADMIN_AUTH_KEY') ) && $this->is_suppierids > 0){
				$this->listmap .= ' and supplier_id in('.$this->suppierids.')';
				$this->SupplierList = M('Supplier') -> where('site_id='.$this->siteid.' and id in('.$this->suppierids.')') -> select();
				
			}else{  
				$this->SupplierList = M('Supplier') -> where(array('site_id' => $this->siteid)) -> select();
			}
			
			$this->cate_list=D('Product_category')->getListToArray1(0,$this->siteid);
			$id=$this->_get('id');
		    $this->info = $this->dao->where('id='.$id)->find();
			$info = $this->info;
			$info['levelpriceconfig'] = unserialize($info['levelpriceconfig']);
			//dump($info['levelpriceconfig']);
			if(!$info['levelpriceconfig']){
				foreach($this->getLevelNameList as $k => $v){
					$levelpriceconfig[$k]['levelkey'] = 0;
					$levelpriceconfig[$k]['levelprice'] = 0;
					$levelpriceconfig[$k]['levelaff'] = 0;
				}
				$info['levelpriceconfig'] = $levelpriceconfig;
			}
			
			
			$this->numpriceconfig = unserialize($info['numpriceconfig']);
			
			
			$this->assign('info', $info);
			
			
			
			
			if($this->info['mimg_url']!=''){
			    $this->mimg_url_list=explode(',',$this->info['mimg_url']);
			}
			$this->relateids = explode(',', $this->info['relateids']);
			
			$relatelist = array();
			foreach($this->relateids as $k => $v){
				$relatelist[] = $this->dao -> where(array('id' => $v, 'site_id' => $this->siteid)) -> find();
			}
			$this->assign('relatelist', $relatelist);
			//固定栏目及字段扩展
			$SiteModel=M('Site');
			$curTemplate=$SiteModel->where('id='.$this->siteid)->Field("productExtendsField as productExtendsField")->find();
			
			$this->extendsField=json_decode($curTemplate['productExtendsField'],true);
			//读取字段扩展数据
			$curexval=M('Product_extends')->where(array('product_id'=>$this->_get('id')))->select();
			foreach($curexval as $k=>$v){
				$extendsField_val[$v['name']]=$v['val'];
			}
			$this->assign('extendsField_val',$extendsField_val);
			
			if($this->info['systemagent'] > 0){
				$agentpricedb = D('Agentprice');
				$this->agentprice = json_decode($agentpricedb -> getsaleprice($this->info['systemagent']),true);
				$this->discount = $agentpricedb -> discount;
				$systemagentprice = json_decode($this->info['systemagentprice'],true);
				if($systemagentprice){
					$this->systemagentprice = $systemagentprice;
				}else{
					$this->systemagentprice = $this->agentprice;
				}
				
				

				$this->agentrenewprice = json_decode($agentpricedb -> getsalerenewprice($this->info['systemagent']),true);
				//$this->discount = $agentpricedb -> discount;
				$systemagentrenewprice = json_decode($this->info['systemagentrenewprice'],true);
				if($systemagentrenewprice){
					$this->systemagentrenewprice = $systemagentrenewprice;
				}else{
					$this->systemagentrenewprice = $this->agentrenewprice;
				}
				//dump($this->systemagentrenewprice);
				
				
				$this->display('agent');exit;
			}
			
			$this->display();
		}else{
			$this->error('参数错误');
		}
	}
	
	public function update(){
	    if($this->_post('id')){
			if($this->_post('mimg_url')){
				$_POST['mimg_url']=implode(',',$_POST['mimg_url']);
			}else{
				$_POST['mimg_url']='';
			}
			if($_POST['supplier_id'] > 0){
				//查询这个供货商，把它的坐标写到商品表中
				$supplier = M('Supplier') -> where(array('id' => $_POST['supplier_id'])) -> find();
				$_POST['baidumapX'] = $supplier['baidumapX'];
				$_POST['baidumapY'] = $supplier['baidumapY'];
				$_POST['geohash'] = $supplier['geohash'];
			}
			$_POST['relateids'] = implode(',', $_POST['relateids']);
			
			$cgdb = M('Category_goods');
			$this->scategorylist = $cgdb -> where(array('product_id' => $this->_post('id'))) -> delete();
			//保存扩展分类
			foreach($_POST['category_goods'] as $k){
				$cgdb -> data(array('category_id' => $k, 'product_id' => $this->_post('id'))) -> add();
			}

			if($this->_post('starttime') != ''){
				$_POST['starttime'] = strtotime($this->_post('starttime'));
			}
			if($this->_post('timeout') != ''){
				$_POST['timeout'] = strtotime($this->_post('timeout'));
			}
			
			if($this->_post('extendsField')){
				$newexdb=M('Product_extends');
			    $oldData=$newexdb->where(array('product_id'=>$this->_post('id')))->select();
				foreach($oldData as $k=>$v){
					$oldname[]=$v['name'];
				}
				//获取老的扩展数据,通过对比,更新或写入新数据
				
				foreach($_POST['extendsField'] as $k=>$v){
					
					$u['name']=$k;
					$u['val']=$v;
					$u['product_id']=$this->_post('id');
					if(in_array($k,$oldname)){
						$newexdb->where(array('product_id'=>$this->_post('id'),'name'=>$k))->data($u)->save();
					}else{
						$newexdb->data($u)->add();
					}
					//dump($u);
				}
			}
			
			//写入数量价格配置
			if($this->_post('numkey')){
				$numkey = $this->_post('numkey');
				$numpriceconfig = array();
				$curnum = 0;
				foreach($numkey as $k => $v){
					if($k == 0){
						continue;
					}
					$ret[$_POST['numprice'][$k].','.$_POST['numaff'][$k]] = $_POST['numkey'][$k];
					
				}
				asort($ret);
				//if($this->siteid == 13){dump($ret);exit;}
				foreach($ret as $k => $v){
					$explodekey = explode(',', $k);
					$numconfigdata['numprice'] = (float)$explodekey[0];
					$numconfigdata['numaff'] = (float)$explodekey[1];
					$numconfigdata['numkey'] = (float)$v;
					$numpriceconfig[] = $numconfigdata;
				}
				$_POST['numpriceconfig'] = serialize($numpriceconfig);
			}
			
			
			
			//写入等级价格配置
			if($this->_post('levelprice')){
				$levelkey = $this->_post('levelkey');
				$levelpriceconfig = array();
				foreach($levelkey as $k => $v){
					
					$levelpriceconfig[$k]['levelkey'] = $_POST['levelkey'][$k];
					$levelpriceconfig[$k]['levelprice'] = $_POST['levelprice'][$k];
					$levelpriceconfig[$k]['levelaff'] = $_POST['levelaff'][$k];
				}
				$_POST['levelpriceconfig'] = serialize($levelpriceconfig);
			}
			
			$agentprice = $agentpricerenew = array();
			$ragentprice = json_decode(D('Agentprice') -> getsaleprice($this->info['systemagent']),true);
			
			foreach($_POST['salenum'] as $k => $v){
				if(intval($_POST['salenum'][$k]) == 0 && $ragentprice[0]['num'] != 0){
					$this->error('非法价格！');exit;
				}
				$agentprice[$k]['num'] = $_POST['salenum'][$k];
				$agentprice[$k]['price'] = $_POST['saleprice'][$k];
				$agentprice[$k]['aff'] = $_POST['saleaff'][$k];
			}
			foreach($_POST['renewsalenum'] as $k => $v){
				if(intval($_POST['renewsalenum'][$k]) == 0 && $ragentprice[0]['num'] != 0){
					$this->error('非法价格！');exit;
				}
				$agentpricerenew[$k]['num'] = $_POST['renewsalenum'][$k];
				$agentpricerenew[$k]['price'] = $_POST['renewsaleprice'][$k];
				$agentpricerenew[$k]['aff'] = $_POST['renewsaleaff'][$k];
			}
			
			
			$_POST['systemagentrenewprice'] = json_encode($agentpricerenew);
			
			$_POST['systemagentprice'] = json_encode($agentprice);
			parent::update();
		}
	}
	//编辑属性
	public function editAttr(){
		//获取提交的商品ID
		if($this->_get('id')){
			$id=$this->_get('id');
			//找到一个产品
		    $cur_product=$this->dao->where(array('site_id'=>$this->siteid,'id'=>$id))->find();
			//这个商品已经不存在了
			if(!array($cur_product)){
				$this->error('找不到这个商品');
			}
			//查找属性,联合类型表和分类表
			
			
			
			$aamap = array('t.site_id'=>$this->siteid,'pc.id'=>$cur_product['category_id']);
				if ($this->is_suppierids > 0) {
					$aamap['a.user_id'] = array('in', '0,'.session(C('USER_AUTH_KEY')));
				}else{
					//$aamap['a.user_id'] = 0;
				}
			
			
			$attrModel=M("Attr");
			$attrList=$attrModel->alias('a')->join("left join __TYPE__ as t on t.id=a.type_id")->join("left join __PRODUCT_CATEGORY__ as pc on pc.type_id=t.id")->where($aamap)->Field("a.*")->select();
			//查找每个属性的可选值
			$attr_valModel=M('Attr_val');
			foreach($attrList as $k=>$v){
				$amap = array('site_id'=>$this->siteid,'attr_id'=>$v['id']);
				if ($this->is_suppierids > 0) {
					$amap['user_id'] = array('in', '0,'.session(C('USER_AUTH_KEY')));
				}else{
					//$amap['user_id'] = 0;
				}
				$attrList[$k]['valList']=$attr_valModel->where($amap)->order('sort')->select();
			}
			//读取老的属性和规格列表
			$stock=M('Product_stock')->where(array('product_id'=>$id))->select();
			$curattr=M('Product_attr')->where(array('product_id'=>$id))->Field('attr_val_id')->select();
			foreach($curattr as $k=>$v){
				$curattr1[]=$v['attr_val_id'];
			}
			unset($curattr);
			$this->assign('stock',$stock);
			$this->assign('curattr',$curattr1);
			//得到当前商品可选属性和属性值列表
			$this->assign('attrList',$attrList);
			//当前商品ID
			$this->assign('id',$id);
			$this->display();
		}
		
	}
	
	//单文件上传
	public function attrupload(){
		if(isset($_FILES['file'])){
			$this->name=isset($_POST['name'])?$_POST['name']:'';
			$info=$this->_upload('file');
				exit("<script>window.parent.uploaded('".$info."','".$this->name."'); location.href='".U('upload',array('name'=>$this->name))."';</script>");
		}else{
			$this->name=$this->_get('name');
			$this->display('Public:upload');
		}
	}
	
	//写入sku信息
	public function insertSKU(){
		if(isset($_POST['product_id']) && intval($_POST['product_id'])>0){
			 $id=$this->_post('product_id');
		     $stockModel=M("Product_stock");
			 $attrModel=M("Product_attr");
			 //删除旧记录
			 $stockModel->where(array('product_id'=>$id))->delete();
			 $attrModel->where(array('product_id'=>$id))->delete();
			 $reprice=$this->_post('price');
			 $resocre=$this->_post('socre');
			 $attrList=$this->_post('attrVal');
			 $reaffmoney=$this->_post('reaffmoney');
			 $stock=$this->_post('stock');
			 $num=$this->_post('num');
			 $code=$this->_post('code');
			 $cost=$this->_post('cost');
			
			 //写入库存
			 $stocks = 0;
			 foreach($attrList as $k=>$v){
				 $stockdata['reprice']=$reprice[$k];
				 $stockdata['resocre']=$resocre[$k];
				 $stockdata['attrList']=$attrList[$k];
				 $stockdata['stock']=$stock[$k];
				 $stockdata['cost']=$cost[$k];
				 $stocks += $stock[$k]*1;
				 $stockdata['reaffmoney']=$reaffmoney[$k];
				 $stockdata['num']=$num[$k];
				 $stockdata['code']=$code[$k];
				 $stockdata['product_id']=$id;
				 $stockdata['site_id']=$this->siteid;
				 $stockModel->add($stockdata);
			 }
			 unset($stockdata);
			 //写入筛选属性
			 $attrList=$this->_post('attrList');
			 foreach($attrList as $k=>$v){
				 $attr_val=explode('_',$v);
				 $attrkdata['attr_id']=$attr_val[0];
				 $attrkdata['attr_val_id']=$attr_val[1];
				 $attrkdata['product_id']=$id;
				 $attrkdata['site_id']=$this->siteid;
				 $attrModel->add($attrkdata);
			 }
			 
			 
			 $this->dao -> where(array('id' => $id)) -> save(array('stocks' => $stocks));
			 
			 $this->success('操作完成',session('jumpUrl_'));
		}
	}
	
	
	//显示子产品列表
	public function showSeeds(){
		
		$this->listmap="1 and n.site_id=".$this->siteid;
		$this->listorder="sort,id";
		if(isset($_GET['category_id'])&&intval($_GET['category_id'])>0){
			$this->listmap.=" and n.category_id=".intval($_GET['category_id']);
			$this->category_id=intval($_GET['category_id']);
		}
		
		//搜索标题
		if(isset($_GET['keywords'])&& $_GET['keywords']!=''){
			$keywords=$_GET['keywords'];
			$_GET['keywords']=urlencode($_GET['keywords']);
			$this->listmap.=" and n.title like '%{$keywords}%'";
			$this->keywords=$keywords;
		}
		
		$this->categoryList=M('Product_category')->where(array('site_id'=>$this->siteid))->select();
        $this->_list();
		$this->display();
		
		
	}
	
	
	public function selectNews(){
		//增加记录
		if($this->_post('id')){
			$modulesdb = M('Modules');
			$id = $this->_post('id');
			$module = $modulesdb->where(array('id' => $this->_post('modules_id'),'site_id' => $this->siteid))->find();
			//echo $modulesdb->getLastSql();
			if(is_array($module)){
				$modules = unserialize($module['content']);
				$newdata = array();
				
				$isex = false;
				foreach($modules as $k => $v){
					if($v['id'] != $id || $v['module'] == 0){
					    $newdata[] = $v;
					}else{
						$isex = true;
					}
					//是否已经存在记录,是就取消
				}
				//记录不存在增加一条记录
				if(!$isex){
				    $newdata[] = $this->dao->where(array('id' => $id,'site_id' => $this->siteid))->find();
				}
				$ret = $modulesdb->data(array('content' => serialize($newdata)))->where(array('id' => $this->_post('modules_id'),'site_id' => $this->siteid))->save();
				if($ret !== false){
					$data['status'] = 1;
					$data['info'] = '操作成功';
					if(!$isex){
						//新增
						$data['type'] = 1;
					}else{
						$data['type'] = 0;
					}
					exit(json_encode($data));
				}else{
					$this->error($modulesdb->getError());
				}
				
			}else{
				$this->error('当前站点没有这个模块');
			}
			exit();
		}
		if($this->_request('modules_id')){
			$module = M('Modules')->where(array('id' => $this->_get('modules_id'),'site_id' => $this->siteid))->find();
			if(is_array($module)){
				$modules = unserialize($module['content']);
				$selectedid = array();
				foreach($modules as $k => $v){
					if($v['module'] == 1)
					$selectedid[] = $v['id'];
				}
				$this->modules = $modules;
				$this->assign('modules_id', $this->_get('modules_id'));
				$this->assign('selectedid',$selectedid);
			}else{
				$this->error('当前站点没有这个模块');
			}
		}else{
			$this->error('模块ID出错,参数错误');exit;
		}
		$category=D('Product_category');
		
		
		
		$siteinfo = M('Site') -> where(array('id' => $this->siteid)) -> find();
		if($siteinfo['pagerelate'] > 0 && $siteinfo['relate_id'] > 0){
			$this->siteid = $siteinfo['relate_id'];
		}
		$this->listmap="1 and n.site_id=".$this->siteid;
		$this->listorder="sort,id";
		
		if(isset($_GET['category_id'])&&intval($_GET['category_id'])>0){
			$cateids = $category -> getAllIDBypid(intval($_GET['category_id']), $this->siteid);
			$this->listmap.=" and n.category_id in(".implode(',',$cateids).")";
			//dump($cateids);
			$this->category_id=intval($_GET['category_id']);
		}
		
		//搜索标题
		//dump($_GET);
		if($this->_param('keywords')){
			$keywords=$this->_param('keywords');
			$_GET['keywords']=urlencode($this->_param('keywords'));
			$this->listmap.=" and n.title like '%{$keywords}%'";
			$this->keywords=$keywords;
		}
		//栏目筛选
		if(isset($_REQUEST['status']) && intval($_REQUEST['status']) > 0){
			$this->listmap .= " and n.status=1";
			$this->status = 1;
		}
		if(isset($_REQUEST['is_top']) && intval($_REQUEST['is_top']) > 0){
			$this->listmap .= " and n.is_top=1";
			$this->is_top = 1;
		}
		if(isset($_REQUEST['is_hot']) && intval($_REQUEST['is_hot']) > 0){
			$this->listmap .= " and n.is_hot=1";
			$this->is_hot = 1;
		}
		if(isset($_REQUEST['is_rec']) && intval($_REQUEST['is_rec']) > 0){
			$this->listmap .= " and n.is_rec=1";
			$this->is_rec = 1;
		}
		
		
		$pid_list=$category->where(array('pid'=>0,'site_id'=>$this->siteid))->select();
		
		foreach($pid_list as $k=>$v){
			$pid_list[$k]['sub']=$category->getListToArray1($v['id'],$this->siteid);
		}
		$this->assign('pid_list',$pid_list);
        $this->_list();
		if(isMobile()){
			$this->display('selectNewsMobile');
		}else{
			$this->display();
		}
	
	}
	
	//移动到供货商
	public function moveSupplier(){
		if($this->_get('supplier_id')){
			$category_id=$this->_get('supplier_id');
			$ids=$this->_get('id');
			$map['id'] = array('in', $ids);
			$data['supplier_id']=$category_id;
			$re = $this->dao->where($map)->data($data)->save();
			if($re !== false){
			    $this->success('成功移动',U('index'));
			}else{
				$this->error($this->dao->getError());
			}
		}else{
			$this->error('参数错误，没有选择供货商！');
		}
	}
	
	//移动到分类
	public function move(){
		if($this->_get('category_id')){
			$category_id=$this->_get('category_id');
			$ids=$this->_get('id');
			$map['id'] = array('in', $ids);
			$data['category_id']=$category_id;
			$re = $this->dao->where($map)->data($data)->save();
			if($re !== false){
			    $this->success('成功移动',U('index'));
			}else{
				$this->error($this->dao->getError());
			}
		}else{
			$this->error('参数错误，没有选择分类！');
		}
	}
}